/********************************************************************************
* Project       : No Latency RTSP streaming Project
* Author        : Seo, SeokWon
* e-mail        : kueony@gmail.com
* Licence       : See the Copyright
*********************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "lib_str.h"



int CopyStrByBlankWithMax(char* a_pcSrc, char* a_pcDest, int iMaxLen)
{
	int i;
	char c;

	for(i = 0, c=a_pcSrc[i] ; i < iMaxLen && c!=' ' ; i++, c=a_pcSrc[i])
	{
		a_pcDest[i] = c;
	}

	return i;
}

int GetValueByTag(char *a_pcSource, char *a_pcTag, char *a_pcValue)
{
    char	caStartTag[64] = {0, };
    char	caEndTag[64] = {0, };
	char	*pcStart = NULL;
	char	*pcEnd = NULL;

	if(strlen(a_pcTag) > 62)
	{
		return -1;
	}

    memset(caStartTag, 0, sizeof(caStartTag));
    memset(caEndTag, 0, sizeof(caEndTag));

	sprintf(caStartTag, "<%s>", a_pcTag);
	sprintf(caEndTag, "</%s>", a_pcTag);

	if((pcStart = strstr(a_pcSource, caStartTag)) == NULL) return -1;
	if((pcEnd = strstr(a_pcSource, caEndTag)) == NULL) return -1;

	pcStart = pcStart + strlen(caStartTag);

    memcpy(a_pcValue, pcStart, pcEnd - pcStart);

    return 0;
}


int GetValueByIdInTag(char *a_pcStr, char *a_pcCmpStr, char *a_pcValue)
{
	char    *pcBuf = NULL;
	int		iStart = 0;
	int		iSize = 0;
	int		iCheckFirst = 0;
	int		i;
	
	pcBuf = strstr(a_pcStr, a_pcCmpStr);
	
	if(pcBuf == NULL) return -1;
	
	for(i = 0 ; i < (int)strlen(pcBuf) ; i++)
	{
		if(pcBuf[i] == 34 || pcBuf[i] == 39)
		{
			if(iCheckFirst == 0)
			{
				iStart = i + 1;
				iCheckFirst = 1;
			}
			else
			{
				iSize = i - iStart;
				break;
			}
		}
	}
	
	if(iSize < 1) return -1;
	
	memcpy(a_pcValue, pcBuf + iStart, iSize);
	
	return 0;
}



int GetValueByToken(char *a_pcSource, int a_iCheckNum, char *a_pcValue, char *a_pcToken)
{
	char	caSource[512] = {0, };
    char    *pcStr = NULL;
    char    *pcBuf = NULL;
    int     iTokenCount = 0;
    int     iCheckOk = 0;

    if(a_iCheckNum < 1) return -1;
	
	sprintf(caSource, "%.512s", a_pcSource);

    if((pcStr = strtok_r(caSource, a_pcToken, &pcBuf)) == NULL) return -1;

    do
    {
        iTokenCount++;

        if(iTokenCount == a_iCheckNum)
        {
            memcpy(a_pcValue, pcStr, strlen(pcStr));
            iCheckOk = 1;
            break;
        }
    } while ((pcStr = strtok_r(NULL, a_pcToken, &pcBuf)) != NULL);

    if(iCheckOk == 0) return -1;

    return 0;
}


int TrimStr(char *a_pcStr)
{
    int		iLen = 0;
    int		iBlankCount = 0;
    char	*pcResult;
    int		i;

    iLen = strlen(a_pcStr);

    if(iLen < 1) return -1;

    for(i = 0 ; i < iLen ; i++)
    {
        if(a_pcStr[i] < 33) a_pcStr[i] = ' ';
    }

    pcResult = (char *) malloc (iLen);

    memset(pcResult, 0, iLen);

    for(i = 0 ; i < iLen ; i++)
    {
        if(a_pcStr[i] != ' ')
        {
            pcResult[iBlankCount++] = a_pcStr[i];
        }
    }

    memset(a_pcStr, 0, iLen);
    memcpy(a_pcStr, pcResult, iBlankCount);

    free(pcResult);

    return 0;
}

																																		 
// about file //

int	GetValueByTagInFile(char *a_pcFileName, char *a_pcTag, char *a_pcValue)
{
	FILE	*fp = NULL;
    char	caBuf[512] = {0, };
    char	*pcCheck;
	int		iCheckOk = 0;

    fp = fopen(a_pcFileName, "r");

    if(fp == NULL)
    {
        return -1;
    }

    while(!feof(fp))
    {
        memset(caBuf, 0, sizeof(caBuf));
        fgets(caBuf, sizeof(caBuf), fp);

        if(strlen(caBuf) < 1) continue;

        TrimStr(caBuf);

        if(caBuf[0] == '#') continue;

        if(strncmp(caBuf, a_pcTag, strlen(a_pcTag)) == 0)
        {
			if(caBuf[strlen(a_pcTag)] != '=') continue;

			pcCheck = caBuf + strlen(a_pcTag) + 1;

			if(strlen(pcCheck) < 1) continue;

			memcpy(a_pcValue, pcCheck, strlen(pcCheck));
			
			iCheckOk = 1;
			break;
        }
	}
    
	fclose(fp);

	if(iCheckOk == 0) return -1;

	return 0;
}


int GetValueByStr(char *a_pcSource, char *a_pcStr, char *a_pcValue)
{
	char *pcCheck = NULL;

    if(TrimStr(a_pcSource) < 0) return -1;
	if(a_pcSource[0] == '#') return -1;

    if(strncmp(a_pcSource, a_pcStr, strlen(a_pcStr)) != 0) return -1;
	
	if(a_pcSource[strlen(a_pcStr)] != '=') return -1;

	pcCheck = a_pcSource + strlen(a_pcStr) + 1;

	if(strlen(pcCheck) < 1) return -1;

	memcpy(a_pcValue, pcCheck, strlen(pcCheck));

	return 0;
}


//-----------------------------------------------------------------------------------------



